﻿@(Html.DevExtreme().DataGrid<DevExtreme.NETCore.Demos.Models.DataGrid.EmployeeValidation>()
    .ID("gridContainer")
    .ColumnAutoWidth(true)
    .ShowBorders(true)
    .Paging(paging => paging.Enabled(false))
    .Editing(editing => {
        editing.Mode(GridEditMode.Batch);
        editing.AllowUpdating(true);
        editing.AllowAdding(true);
    })
    .Columns(columns => {
        columns.AddFor(m => m.FirstName);
        columns.AddFor(m => m.LastName);
        columns.AddFor(m => m.Position);
        columns.AddFor(m => m.Phone);
        columns.AddFor(m => m.Email)
            .ValidationRules(vr => {
                vr.AddAsync()
                    .Message("Email address is not unique")
                    .ValidationCallback("asyncValidation");
            });
    })
    .DataSource(d => d.Mvc().Controller("DataGridEmployeesValidation")
        .LoadAction("Get")
        .InsertAction("Post")
        .UpdateAction("Put")
        .Key("ID")
    )
)

<script>
    function asyncValidation(params) {
        return $.ajax({
            url: '@Url.Action("CheckUniqueEmailAddress", "RemoteValidation")',
            type: "POST",
            dataType: "json",
            contentType: "application/json",
            data: JSON.stringify({
                id: params.data.ID,
                email: params.value
            })
        });
    }
</script>
